(function(){"use strict";BX.namespace("BX.Filter");BX.Filter.Presets=function(e){this.parent=null;this.presets=null;this.container=null;this.init(e)};BX.Filter.Presets.prototype={init:function(e){this.parent=e},bindOnPresetClick:function(){(this.getPresets()||[]).forEach(function(e){BX.bind(e,"click",BX.delegate(this._onPresetClick,this))},this)},getAddPresetField:function(){return BX.Filter.Utils.getByClass(this.getContainer(),this.parent.settings.classAddPresetField)},getAddPresetFieldInput:function(){return BX.Filter.Utils.getByClass(this.getAddPresetField(),this.parent.settings.classAddPresetFieldInput)},clearAddPresetFieldInput:function(){var e=this.getAddPresetFieldInput();e&&(e.value="")},normalizePreset:function(e){if(!BX.hasClass(e,this.parent.settings.classPreset)){e=BX.findParent(e,{className:this.parent.settings.classPreset},true,false)}return e},deactivateAllPresets:function(){var e=this.getPresets();var t=this;(e||[]).forEach(function(e){if(BX.hasClass(e,t.parent.settings.classPresetCurrent)){BX.removeClass(e,t.parent.settings.classPresetCurrent)}})},createSidebarItem:function(e,t,s){return BX.decl({block:"sidebar-item",text:BX.util.htmlspecialcharsback(t),id:e,pinned:s,noEditPinTitle:this.parent.getParam("MAIN_UI_FILTER__IS_SET_AS_DEFAULT_PRESET"),editNameTitle:this.parent.getParam("MAIN_UI_FILTER__EDIT_PRESET_TITLE"),removeTitle:this.parent.getParam("MAIN_UI_FILTER__REMOVE_PRESET"),editPinTitle:this.parent.getParam("MAIN_UI_FILTER__SET_AS_DEFAULT_PRESET"),dragTitle:this.parent.getParam("MAIN_UI_FILTER__DRAG_TITLE")})},activatePreset:function(e){this.deactivateAllPresets();if(BX.type.isNotEmptyString(e)){e=this.getPresetNodeById(e)}if(e&&!BX.hasClass(e,this.parent.settings.classPresetCurrent)){BX.addClass(e,this.parent.settings.classPresetCurrent)}},getPresetNodeById:function(e){var t=this.getPresets();var s=t.filter(function(t){return BX.data(t,"id")===e},this);return s.length>0?s[0]:null},getPresetId:function(e){return BX.data(e,"id")},updatePresetName:function(e,t){var s;if(BX.type.isDomNode(e)&&BX.type.isNotEmptyString(t)){s=this.getPresetNameNode(e);if(BX.type.isDomNode(s)){BX.html(s,t)}}},removePreset:function(e,t,s){var i=this.getCurrentPresetId();var r=[];var a={preset_id:t,is_default:s};var n={FILTER_ID:this.parent.getParam("FILTER_ID"),action:"REMOVE_FILTER"};this.parent.saveOptions(a,n);BX.remove(e);if(BX.type.isArray(this.parent.params["PRESETS"])){r=this.parent.params["PRESETS"].filter(function(e){return e.ID!==t},this);this.parent.params["PRESETS"]=r}if(BX.type.isArray(this.parent.editablePresets)){r=this.parent.editablePresets.filter(function(e){return e.ID!==t},this);this.parent.editablePresets=r}if(t===i){this.parent.getSearch().removePreset();this.resetPreset()}},pinPreset:function(e){if(!BX.type.isNotEmptyString(e)){e="default_filter"}var t=this.getPresetNodeById(e);if(this.parent.getParam("VALUE_REQUIRED_MODE")){if(e==="default_filter"){return}}var s={FILTER_ID:this.parent.getParam("FILTER_ID"),GRID_ID:this.parent.getParam("GRID_ID"),action:"PIN_PRESET"};var i={preset_id:e};this.getPresets().forEach(function(e){BX.removeClass(e,this.parent.settings.classPinnedPreset)},this);BX.addClass(t,this.parent.settings.classPinnedPreset);this.parent.saveOptions(i,s)},_onPresetClick:function(e){var t,s,i,r,a,n,l;e.preventDefault();l=this.parent;n=l.settings;a=e.target;t=e.currentTarget;s=this.getPresetId(t);i=this.getPreset(s);if(BX.hasClass(a,n.classPinButton)){if(this.parent.isEditEnabled()){if(BX.hasClass(t,n.classPinnedPreset)){this.pinPreset("default_filter")}else{this.pinPreset(s)}}}if(BX.hasClass(a,n.classPresetEditButton)){this.enableEditPresetName(t)}if(BX.hasClass(a,n.classPresetDeleteButton)){r="IS_DEFAULT"in i?i.IS_DEFAULT:false;this.removePreset(t,s,r);return false}if(!BX.hasClass(a,n.classPresetDragButton)&&!BX.hasClass(a,n.classAddPresetFieldInput)){if(this.parent.isEditEnabled()){this.updateEditablePreset(this.getCurrentPresetId())}var p=this.getPreset(this.getCurrentPresetId());var h=this.getPreset(s);p.ADDITIONAL=[];h.ADDITIONAL=[];this.activatePreset(t);this.applyPreset(s);if(!this.parent.isEditEnabled()){l.applyFilter(null,true);if(e.isTrusted){l.closePopup()}if(l.isAddPresetEnabled()){l.disableAddPreset()}}}},applyPinnedPreset:function(){var e=this.parent;var t=this.isPinned(this.getCurrentPresetId());var s;if(this.parent.getParam("VALUE_REQUIRED")&&this.getPinnedPresetId()==="default_filter"){this.applyPreset("default_filter");this.deactivateAllPresets();s=this.parent.applyFilter()}else{if(!t){var i=this.getPinnedPresetId();var r=this.getPinnedPresetNode();var a=false;var n=true;this.deactivateAllPresets();this.activatePreset(r);this.applyPreset(i);s=e.applyFilter(a,n);e.closePopup()}else{s=e.resetFilter()}}return s},updateEditablePreset:function(e){var t=this.parent.getFilterFieldsValues();var s=this.getFields().map(function(e){return BX.data(e,"name")});var i=this.parent.preparePresetFields(t,s);var r=this.getPreset(e);r.FIELDS=i;r.TITLE=this.getPresetInput(this.getPresetNodeById(e)).value;r.ROWS=s},getPresetInput:function(e){return BX.Filter.Utils.getByClass(e,this.parent.settings.classPresetEditInput)},enableEditPresetName:function(e){var t=this.getPresetInput(e);BX.addClass(e,this.parent.settings.classPresetNameEdit);t.focus();t.value=BX.util.htmlspecialcharsback(t.value);BX.bind(t,"input",BX.delegate(this._onPresetNameInput,this))},_onPresetNameInput:function(e){var t=this.parent.getSearch();var s=e.currentTarget.value;var i=BX.findParent(e.currentTarget,{className:this.parent.settings.classPreset},true,false);var r=this.getPresetId(i);var a=this.getCurrentPresetId();var n={ID:r,TITLE:s};if(r===a){t.updatePreset(n)}},getPresetNameNode:function(e){return BX.Filter.Utils.getByClass(e,this.parent.settings.classPresetName)},disableEditPresetName:function(e){var t=this.getPresetInput(e);BX.removeClass(e,this.parent.settings.classPresetNameEdit);if(BX.type.isDomNode(t)){t.blur();BX.unbind(t,"input",BX.delegate(this._onPresetNameInput,this))}},getPreset:function(e,t){var s=this.parent.getParam(t?"DEFAULT_PRESETS":"PRESETS",[]);if(this.parent.isEditEnabled()&&!t){s=this.parent.editablePresets}var i=s.filter(function(t){return t.ID===e});if(e==="tmp_filter"&&!i.length){var r=BX.clone(this.getPreset("default_filter"));r.ID="tmp_filter";s.push(r);i.push(r)}return i.length!==0?i[0]:null},getPresetField:function(e,t){var s=this.getPreset(e);var i=null;if(BX.type.isPlainObject(s)&&"FIELDS"in s&&BX.type.isArray(s.FIELDS)){i=s.FIELDS.filter(function(e){return e.NAME===t});i=i.length?i[0]:null}return i},applyPreset:function(e,t){e=t?"default_filter":e||"default_filter";var s=this.getPreset(e);if(e!=="default_preset"){s=this.extendPreset(s)}this.parent.getSearch().updatePreset(s);this.updatePresetFields(s,t)},extendPreset:function(e){var t=BX.clone(this.getPreset("default_filter"));if(BX.type.isPlainObject(e)){e=BX.clone(e);e.FIELDS.forEach(function(e){var s;var i=t.FIELDS.some(function(t,i){var r=false;if(t.NAME===e.NAME){s=i;r=true}return r},this);if(i&&s||i&&s===0){t.FIELDS[s]=e}else{if(!this.isEmptyField(e)){t.FIELDS.push(e)}}},this);e.FIELDS=t.FIELDS}return e},isEmptyField:function(e){var t=true;if(e.TYPE===this.parent.types.STRING){if(e.VALUE&&e.VALUE.length){t=false}}if(e.TYPE===this.parent.types.SELECT){if(BX.type.isPlainObject(e.VALUE)&&"VALUE"in e.VALUE&&e.VALUE.VALUE){t=false}}if(e.TYPE===this.parent.types.MULTI_SELECT){if(BX.type.isArray(e.VALUE)&&e.VALUE.length){t=false}}if(e.TYPE===this.parent.types.CUSTOM_DATE){if(BX.type.isArray(e.VALUE.days)&&e.VALUE.days.length||BX.type.isArray(e.VALUE.months)&&e.VALUE.months.length||BX.type.isArray(e.VALUE.years)&&e.VALUE.years.length){t=false}}if(e.TYPE===this.parent.types.CUSTOM_ENTITY||e.TYPE===this.parent.types.DEST_SELECTOR){if(BX.type.isPlainObject(e.VALUES)){if(BX.type.isNotEmptyString(e.VALUES._label)&&BX.type.isNotEmptyString(e.VALUES._value)){t=false}if(BX.type.isPlainObject(e.VALUES._label)&&BX.type.isPlainObject(e.VALUES._value)&&Object.keys(e.VALUES._label).length&&Object.keys(e.VALUES._value).length){t=false}if(BX.type.isArray(e.VALUES._label)&&BX.type.isArray(e.VALUES._value)&&e.VALUES._label.length&&e.VALUES._value.length){t=false}if((BX.type.isArray(e.VALUES._label)&&e.VALUES._label.length||BX.type.isPlainObject(e.VALUES._label)&&Object.keys(e.VALUES._label).length)&&(BX.type.isArray(e.VALUES._value)&&e.VALUES._value.length||BX.type.isPlainObject(e.VALUES._value)&&Object.keys(e.VALUES._value).length)){t=false}}}if(e.TYPE===this.parent.types.DATE){var s="_datesel"in e.VALUES?e.VALUES._datesel:e.SUB_TYPE.VALUE;if(BX.type.isPlainObject(e.VALUES)&&(e.VALUES._from||e.VALUES._to||e.VALUES._quarter||e.VALUES._month&&!BX.type.isArray(e.VALUES._month)||e.VALUES._year&&!BX.type.isArray(e.VALUES._year)||e.VALUES._days&&!BX.type.isArray(e.VALUES._days))||BX.type.isArray(e.VALUES._days)&&e.VALUES._days.length||BX.type.isArray(e.VALUES._month)&&e.VALUES._month.length||BX.type.isArray(e.VALUES._year)&&e.VALUES._year.length||(s===this.parent.dateTypes.CURRENT_DAY||s===this.parent.dateTypes.CURRENT_WEEK||s===this.parent.dateTypes.CURRENT_MONTH||s===this.parent.dateTypes.CURRENT_QUARTER||s===this.parent.dateTypes.LAST_7_DAYS||s===this.parent.dateTypes.LAST_30_DAYS||s===this.parent.dateTypes.LAST_60_DAYS||s===this.parent.dateTypes.LAST_90_DAYS||s===this.parent.dateTypes.LAST_WEEK||s===this.parent.dateTypes.LAST_MONTH||s===this.parent.dateTypes.TOMORROW||s===this.parent.dateTypes.YESTERDAY||s===this.parent.dateTypes.NEXT_WEEK||s===this.parent.dateTypes.NEXT_MONTH)){t=false}}if(e.TYPE===this.parent.types.NUMBER){if(BX.type.isPlainObject(e.VALUES)&&(e.VALUES._from||e.VALUES._to)){t=false}}if(e.TYPE===this.parent.types.CHECKBOX){if(BX.type.isPlainObject(e.VALUE)&&e.VALUE.VALUE){t=false}}return t},resetPreset:function(e){this.applyPreset("",e)},getFields:function(){var e=this.parent.getFieldListContainer();var t=null;if(BX.type.isDomNode(e)){t=BX.Filter.Utils.getBySelector(e.parentNode,"."+this.parent.settings.classFileldControlList+" > div",true)}return t},getField:function(e){var t=this.getFields();var s=null;var i,r;if(BX.type.isArray(t)&&t.length){r=t.filter(function(t){if(BX.type.isDomNode(t)){i=BX.data(t,"name")}return i===e.NAME},this);s=r.length>0?r[0]:null}return s},removeField:function(e,t){var s,i;t=t||false;if(BX.type.isPlainObject(e)){i=e.NAME;e=this.getField(e);if(BX.type.isArray(this.parent.fieldsList)){s=this.parent.fieldsList.indexOf(e);if(s!==-1){delete this.parent.fieldsList[s]}}this.parent.unregisterDragItem(e)}if(BX.type.isDomNode(e)){i=BX.data(e,"name");this.parent.getFields().deleteField(e)}if(!this.parent.isEditEnabled()&&!this.parent.isAddPresetEnabled()){var r=this.getCurrentPresetId();var a=this.getPresetField(r,i);if(a&&!this.isEmptyField(a)){this.deactivateAllPresets();this.parent.applyFilter()}}if(!t){this.parent.saveFieldsSort()}},removeFields:function(e){e.forEach(function(e){this.removeField(e,true)},this);this.parent.saveFieldsSort()},addField:function(e){var t,s,i;if(BX.type.isPlainObject(e)){t=this.parent.getFieldListContainer();i=this.parent.getControls();s=BX.type.isArray(i)?i[i.length-1]:null;if(BX.type.isDomNode(s)){if(s.nodeName!=="INPUT"){s=BX.Filter.Utils.getByTag(s,"input")}if(BX.type.isDomNode(s)){e.TABINDEX=parseInt(s.getAttribute("tabindex"))+1}}else{e.TABINDEX=2}if(BX.type.isDomNode(t)){s=this.createControl(e);if(BX.type.isDomNode(s)){BX.append(s,t);if(BX.type.isArray(this.parent.fieldsList)){this.parent.fieldsList.push(s)}this.parent.registerDragItem(s)}}}if(!this.parent.isEditEnabled()&&!this.parent.isAddPresetEnabled()){var r=this.getCurrentPresetId();var a=this.getPresetField(r,e.NAME);if(a&&!this.isEmptyField(a)){this.parent.updatePreset("tmp_filter");this.deactivateAllPresets();this.parent.getSearch().updatePreset(this.getPreset("tmp_filter"))}}this.parent.saveFieldsSort()},createControl:function(e){var t;switch(e.TYPE){case this.parent.types.STRING:{t=this.parent.getFields().createInputText(e);break}case this.parent.types.TEXTAREA:{t=this.parent.getFields().createTextarea(e);break}case this.parent.types.SELECT:{t=this.parent.getFields().createSelect(e);break}case this.parent.types.MULTI_SELECT:{t=this.parent.getFields().createMultiSelect(e);break}case this.parent.types.NUMBER:{t=this.parent.getFields().createNumber(e);break}case this.parent.types.DATE:{t=this.parent.getFields().createDate(e);break}case this.parent.types.CUSTOM_DATE:{t=this.parent.getFields().createCustomDate(e);break}case this.parent.types.DEST_SELECTOR:{t=this.parent.getFields().createDestSelector(e);break}case this.parent.types.CUSTOM:{t=this.parent.getFields().createCustom(e);break}case this.parent.types.CUSTOM_ENTITY:{t=this.parent.getFields().createCustomEntity(e);break}default:{break}}if(BX.type.isDomNode(t)){t.dataset.name=e.NAME;t.FieldController=new BX.Filter.FieldController(t,this.parent);if(Boolean(e.REQUIRED)){var s=t.querySelector(".main-ui-filter-field-delete");if(s){BX.remove(s)}}}return t},removeNotCompareVariables:function(e,t){if(BX.type.isPlainObject(e)){var s=this.parent.dateTypes;var i=this.parent.additionalDateTypes;if("FIND"in e){delete e.FIND}if(!t){Object.keys(e).forEach(function(t){if(t.indexOf("_numsel")!==-1){delete e[t]}if(t.indexOf("_datesel")!==-1){var r=e[t];if(r===s.EXACT||r===s.RANGE||r===i.PREV_DAY||r===i.NEXT_DAY||r===i.MORE_THAN_DAYS_AGO||r===i.AFTER_DAYS||r===s.PREV_DAYS||r===s.NEXT_DAYS||r===s.YEAR||r===s.MONTH||r===s.QUARTER||r===s.NONE||r===s.CUSTOM_DATE){delete e[t]}}var a=this.parent.getFieldByName(t);if(e[t]===""&&(!a||!a["STRICT"])){delete e[t]}},this)}}},isPresetValuesModified:function(e){var t=this.getPreset(e);var s=this.parent.preparePresetSettingsFields(t.FIELDS);var i=this.parent.getFilterFieldsValues();this.removeNotCompareVariables(s);this.removeNotCompareVariables(i);var r=BX.Filter.Utils.sortObject(s);var a=BX.Filter.Utils.sortObject(i);return!Object.keys(r).every(function(e){return r[e]===a[e]||(BX.type.isPlainObject(r[e])||BX.type.isArray(r[e]))&&BX.Filter.Utils.objectsIsEquals(r[e],a[e])})},getAdditionalValues:function(e){var t=this.getPreset(e);var s=t.FIELDS.filter(function(e){return!this.isEmptyField(e)},this);var i=this.parent.preparePresetSettingsFields(s);var r=this.parent.getFilterFieldsValues();this.removeNotCompareVariables(i,true);this.removeNotCompareVariables(r,true);this.removeSameProperties(r,i);return r},removeSameProperties:function(e,t){if(BX.type.isPlainObject(e)&&BX.type.isPlainObject(t)){Object.keys(t).forEach(function(t){if(t in e){delete e[t]}})}},removeAdditionalField:function(e){var t=this.getPreset(this.getCurrentPresetId());if(BX.type.isArray(t.ADDITIONAL)){t.ADDITIONAL=t.ADDITIONAL.filter(function(t){return t.NAME!==e})}},updatePresetFields:function(e,t){var s,i;var r=[];if(BX.type.isPlainObject(e)&&"FIELDS"in e){s=e.FIELDS;if(BX.type.isArray(e.ADDITIONAL)){e.ADDITIONAL.forEach(function(e){var t=false;e.IS_PRESET_FIELD=true;s.forEach(function(i,r){if(e.NAME===i.NAME){s[r]=e;t=true}});if(!t){s.push(e)}})}(s||[]).forEach(function(e,s){e.TABINDEX=s+1;if(t){switch(e.TYPE){case this.parent.types.SELECT:{e.VALUE=e.ITEMS[0];break}case this.parent.types.MULTI_SELECT:{e.VALUE=[];break}case this.parent.types.DATE:{e.SUB_TYPE=e.SUB_TYPES[0];e.VALUES={_from:"",_to:"",_days:""};break}case this.parent.types.CUSTOM_DATE:{e.VALUE={days:[],months:[],years:[]};break}case this.parent.types.NUMBER:{e.SUB_TYPE=e.SUB_TYPES[0];e.VALUES={_from:"",_to:""};break}case this.parent.types.CUSTOM_ENTITY:{e.VALUES={_label:"",_value:""};break}case this.parent.types.CUSTOM:{e._VALUE="";break}default:{if("VALUE"in e){if(BX.type.isArray(e.VALUE)){e.VALUE=[]}else{e.VALUE=""}}break}}}r.push(this.createControl(e))},this);this.parent.disableFieldsDragAndDrop();i=this.parent.getFieldListContainer();BX.cleanNode(i);if(r.length){r.forEach(function(t,r){if(BX.type.isDomNode(t)){if(e.ID!=="tmp_filter"&&e.ID!=="default_filter"&&!("IS_PRESET_FIELD"in s[r])&&!this.isEmptyField(s[r])){BX.addClass(t,this.parent.settings.classPresetField)}BX.append(t,i);if(BX.type.isString(s[r].HTML)){var a=BX.create("div");this.parent.getHiddenElement().appendChild(a);BX.html(a,s[r].HTML)}}},this);this.parent.enableFieldsDragAndDrop()}}},showCurrentPresetFields:function(){var e=this.getCurrentPresetData();this.updatePresetFields(e)},getCurrentPreset:function(){return BX.Filter.Utils.getByClass(this.getContainer(),this.parent.settings.classPresetCurrent)},getCurrentPresetId:function(){var e=this.getCurrentPreset();var t=null;if(BX.type.isDomNode(e)){t=this.getPresetId(e)}else{t="tmp_filter"}return t},getCurrentPresetData:function(){var e=this.getCurrentPresetId();var t=null;if(BX.type.isNotEmptyString(e)){t=this.getPreset(e);t=this.extendPreset(t)}return t},getContainer:function(){return BX.Filter.Utils.getByClass(this.parent.getFilter(),this.parent.settings.classPresetsContainer)},getPresets:function(){return BX.Filter.Utils.getByClass(this.getContainer(),this.parent.settings.classPreset,true)},getDefaultPresets:function(){return BX.Filter.Utils.getByClass(this.getContainer(),this.parent.settings.classDefaultFilter,true)},getPinnedPresetNode:function(){return BX.Filter.Utils.getByClass(this.getContainer(),this.parent.settings.classPinnedPreset)},isPinned:function(e){return this.getPinnedPresetId()===e},getPinnedPresetId:function(){var e=this.getPinnedPresetNode();var t="default_filter";if(!!e){var s=BX.data(e,"id");t=!!s?s:t}return t}}})();
//# sourceMappingURL=presets.map.js